' Template Provided By
' Code Endeavors, LLC - http://www.codeendeavors.com

Imports System.Web.Script.Serialization
Imports DotNetNuke
Imports DotNetNuke.Common
Imports DotNetNuke.UI.Utilities

Namespace Modules.$safeprojectname$

    '-------------------------------------------------------------------------------------------------------------
    '- The ControlMethodClass attribute identifies the class as supporting Control Methods
    '- The Friendly name parameter is usually named after server-side class namespace, but could be shortened if you like
    '- If you change this make sure you also change its client calling code found in Edit$safeprojectname$.ascx.js
    '-   dnn.xmlhttp.callControlMethod('$YourCompany$.Modules.$safeprojectname$.Edit$safeprojectname$.' + this._ns, 
    '-------------------------------------------------------------------------------------------------------------
    <ControlMethodClass("$YourCompany$.Modules.$safeprojectname$.Edit$safeprojectname$")> _
    Partial Class Edit$safeprojectname$
        Inherits AjaxPortalModuleBase

#Region "AjaxPortalModuleBase Events"
        '-------------------------------------------------------------------------------------------------------------
        '- This is where you add your custom properties that are to be sent down to the client-side object
        '- By default we are passing the naming container ID (ns) and localized messages (msgs) 
        '- If you add your own properties make sure you update your client code with those properties found in Edit$safeprojectname$.ascx.js
        '-      get_ImagePath: function() {return this._imagePath;},
        '-      set_ImagePath: function(value) {this._imagePath = value;},
        '-------------------------------------------------------------------------------------------------------------
        Protected Sub AjaxPortalModuleBase_AddScriptComponentDescriptors(ByVal Descriptor As ScriptComponentDescriptor) Handles MyBase.AddScriptComponentDescriptors
            'IMPORTANT!  Enter Client Namespace + ObjectName as Type
            Descriptor.Type = "$YourCompany$.Edit$safeprojectname$"

            '---------------------------------------------------------------------------------------------------------
            'Add custom properties here
            '   Descriptor.AddScriptProperty("ImagePath", String.Format("'{0}'", Me.ModulePath & "images/"))

            If Not Settings Is Nothing Then
                'This sample uses the Module Settings collection as a custom property serialized to JSON
                Dim jss As JavaScriptSerializer = New JavaScriptSerializer()
                Descriptor.AddScriptProperty("settings", jss.Serialize(Settings))
            End If
            '---------------------------------------------------------------------------------------------------------
        End Sub

        '-------------------------------------------------------------------------------------------------------------
        '- This is where your client-side javascript that uses the MS AJAX framework needs to be registered
        '- Adding the reference here ensures that the MS AJAX script is run before our script which uses things like Type.registerNamespace run
        '-------------------------------------------------------------------------------------------------------------
        Protected Sub AjaxPortalModuleBase_AddScriptReferences(ByVal References As List(Of ScriptReference)) Handles MyBase.AddScriptReferences
            References.Add(New System.Web.UI.ScriptReference(Me.ModulePath & "Edit$safeprojectname$.ascx.js"))
        End Sub

        '-------------------------------------------------------------------------------------------------------------
        '- Add any localized text needed on the client
        '-------------------------------------------------------------------------------------------------------------
        Protected Sub AjaxPortalModuleBase_AddLocalizedMessages(ByVal Messages As Dictionary(Of String, String)) Handles MyBase.AddLocalizedMessages
            Messages("SettingsSaved") = Localization.GetString("SettingsSaved.Client", LocalResourceFile)
        End Sub

#End Region

#Region "Event Handlers"

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                cmdCancel.NavigateUrl = NavigateUrl()
            Catch exc As Exception        'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

#End Region


#Region "Control Methods"
        '-------------------------------------------------------------------------------------------------------------
        '- Exposing methods to the client code is as simple as adding the ControlMethod attribute
        '- IMPORTANT!   ALWAYS SECURE YOUR CONTROL METHODS IF INTERACTING WITH SENSITIVE DATA
        '-------------------------------------------------------------------------------------------------------------
        <ControlMethod()> _
        Public Function UpdateSettings(ByVal Settings As Dictionary(Of String, Object)) As Boolean
            If Me.EditMode Then 'Making sure only those users with Edit Mode rights can call this method
                Dim modController As DotNetNuke.Entities.Modules.ModuleController = New DotNetNuke.Entities.Modules.ModuleController()
                For Each key As String In Settings.Keys
                    modController.UpdateModuleSetting(Me.ModuleId, key, Settings(key))
                Next
            End If
            Return True
        End Function
#End Region

    End Class

End Namespace